home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Draw / Sources / PalFrame.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  9.5 KB  |  343 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                PalFrame.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:            (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef PALFRAME_H
  15. #include "PalFrame.h"
  16. #endif
  17.  
  18. #ifndef DRAWPART_H
  19. #include "DrawPart.h"
  20. #endif
  21.  
  22. #ifndef UTILS_H
  23. #include "Utils.h"
  24. #endif
  25.  
  26. #ifndef DEFINES_K
  27. #include "Defines.k"
  28. #endif
  29.  
  30. // ----- Part Layer -----
  31.  
  32. #ifndef FWUTIL_H
  33. #include "FWUtil.h"
  34. #endif
  35.  
  36. #ifndef FWCONTXT_H
  37. #include "FWContxt.h"
  38. #endif
  39.  
  40. // ----- OS Layer -----
  41.  
  42. #ifndef FWWINDOW_H
  43. #include "FWWindow.h"
  44. #endif
  45.  
  46. #ifndef FWEVENT_H
  47. #include "FWEvent.h"
  48. #endif
  49.  
  50. #ifndef FWLINSHP_H
  51. #include "FWLinShp.h"
  52. #endif
  53.  
  54. #ifndef FWRECT_H
  55. #include "FWRect.h"
  56. #endif
  57.  
  58. #ifndef FWRECSHP_H
  59. #include "FWRecShp.h"
  60. #endif
  61.  
  62. #ifndef FWMEMMGR_H
  63. #include "FWMemMgr.h"
  64. #endif
  65.  
  66. //========================================================================================
  67. // Runtime Information
  68. //========================================================================================
  69.  
  70. #ifdef FW_BUILD_MAC
  71. #pragma segment odfdrawframes
  72. #endif
  73.  
  74. FW_DEFINE_AUTO(CPaletteFrame)
  75.     
  76. //========================================================================================
  77. // CLASS CPalette
  78. //========================================================================================
  79.  
  80. //----------------------------------------------------------------------------------------
  81. // CPalette::CPalette
  82. //----------------------------------------------------------------------------------------
  83.  
  84. CPalette::CPalette() :
  85.     fColorTable(NULL)
  86. {
  87. #ifdef FW_BUILD_MAC
  88.     fColorTable = ::GetCTable(8);
  89.     fNumbersOfColors = 256;
  90. #endif
  91.  
  92. #ifdef FW_BUILD_WIN
  93.     HDC hdc = ::GetDC(NULL);
  94. /*    if (::GetDeviceCaps(hdc,  RASTERCAPS) & RC_PALETTE)
  95.     {
  96.         fNumbersOfColors = ::GetDeviceCaps(hdc, SIZEPALETTE);
  97.         fColorTable = new PALETTEENTRY[fNumbersOfColors];
  98.         ::GetSystemPaletteEntries(hdc, 0, fNumbersOfColors, fColorTable);
  99.     }
  100.     else*/
  101.     {
  102.         fNumbersOfColors = 1 << (::GetDeviceCaps(hdc, PLANES) * ::GetDeviceCaps(hdc, BITSPIXEL));
  103.         if (fNumbersOfColors < 16)
  104.             fNumbersOfColors = 16;
  105.         if (fNumbersOfColors > 256)
  106.             fNumbersOfColors = 256;
  107.         
  108.         fColorTable = new PALETTEENTRY[fNumbersOfColors];
  109.         BYTE red, green, blue;
  110.         red = green = blue = 0;
  111.         
  112.         BYTE redInc, greenInc, blueInc;
  113.         if (fNumbersOfColors == 16)
  114.         {
  115.             redInc = 64;
  116.             greenInc = 128;
  117.             blueInc = 128;        
  118.         }
  119.         else
  120.         {
  121.             redInc = 32;
  122.             greenInc = 32;
  123.             blueInc = 64;        
  124.         }
  125.         
  126.         for (short i = 0; i < fNumbersOfColors; i++)
  127.         {
  128.             fColorTable[i].peRed = red;
  129.             fColorTable[i].peGreen = green;
  130.             fColorTable[i].peBlue = blue;            
  131.             fColorTable[i].peFlags = 0;
  132.             if (!(blue += blueInc))
  133.                 if (!(red += redInc))
  134.                     green += greenInc;
  135. /*
  136.             if (!(red += redInc))
  137.                 if (!(green += greenInc))
  138.                     blue += blueInc;
  139. */
  140.         }
  141.         fColorTable[fNumbersOfColors-1].peRed = 255;
  142.         fColorTable[fNumbersOfColors-1].peGreen = 255;
  143.         fColorTable[fNumbersOfColors-1].peBlue = 255;            
  144.     }
  145.     ::ReleaseDC(NULL, hdc);
  146. #endif
  147. }
  148.  
  149. //----------------------------------------------------------------------------------------
  150. // CPalette::~CPalette
  151. //----------------------------------------------------------------------------------------
  152.  
  153. CPalette::~CPalette()
  154. {
  155.     if (fColorTable)
  156. #ifdef FW_BUILD_MAC
  157.         ::DisposeCTable(fColorTable);
  158. #endif
  159. #ifdef FW_BUILD_WIN
  160.         delete[] fColorTable;
  161. #endif    
  162.     fColorTable = NULL;
  163. }
  164.  
  165. //----------------------------------------------------------------------------------------
  166. // CPalette::GetColor
  167. //----------------------------------------------------------------------------------------
  168.  
  169. void CPalette::GetColor(short colorIndex, FW_CColor* color) const
  170. {
  171. #ifdef FW_BUILD_MAC
  172.     *color = (*fColorTable)->ctTable[colorIndex].rgb;
  173. #endif
  174. #ifdef FW_BUILD_WIN
  175.     if (colorIndex < fNumbersOfColors)
  176.         (*color).SetRGB(fColorTable[colorIndex].peRed,
  177.                         fColorTable[colorIndex].peGreen,
  178.                         fColorTable[colorIndex].peBlue);
  179.     else
  180.         (*color).SetRGB(128, 128, 128);
  181. #endif
  182. }
  183.  
  184. //========================================================================================
  185. // CLASS CColorChangedInterest
  186. //========================================================================================
  187.  
  188. //----------------------------------------------------------------------------------------
  189. // CColorChangedInterest::CColorChangedInterest
  190. //----------------------------------------------------------------------------------------
  191.  
  192. CColorChangedInterest::CColorChangedInterest(FW_MNotifier* notifier) :
  193.     FW_CInterest(notifier, kColorChanged)
  194. {
  195. }
  196.  
  197. //----------------------------------------------------------------------------------------
  198. // CColorChangedInterest::~CColorChangedInterest
  199. //----------------------------------------------------------------------------------------
  200.  
  201. CColorChangedInterest::~CColorChangedInterest()
  202. {
  203. }
  204.  
  205. //========================================================================================
  206. // CLASS CColorChangedNotification
  207. //========================================================================================
  208.  
  209. //----------------------------------------------------------------------------------------
  210. // CColorChangedNotification::CColorChangedNotification
  211. //----------------------------------------------------------------------------------------
  212.  
  213. CColorChangedNotification::CColorChangedNotification(const FW_CInterest& interest, 
  214.                                                         const FW_CColor& color, 
  215.                                                         FW_ERenderVerbs renderVerb) :
  216.     FW_CNotification(interest),
  217.     fColor(color),
  218.     fRenderVerb(renderVerb)
  219. {
  220. }
  221.  
  222. //----------------------------------------------------------------------------------------
  223. // CColorChangedNotification::~CColorChangedNotification
  224. //----------------------------------------------------------------------------------------
  225.  
  226. CColorChangedNotification::~CColorChangedNotification()
  227. {
  228. }
  229.  
  230. //========================================================================================
  231. // CLASS CPaletteFrame
  232. //========================================================================================
  233.  
  234. //----------------------------------------------------------------------------------------
  235. // CPaletteFrame::CPaletteFrame
  236. //----------------------------------------------------------------------------------------
  237.  
  238. CPaletteFrame::CPaletteFrame(Environment* ev, ODFrame* odFrame, FW_CPresentation* presentation, FW_CPart* part) :
  239.     CFloatingWindowFrame(ev, odFrame, presentation, part),
  240.     FW_MNotifier(),
  241.     fPalette(NULL),
  242.     fGrid(NULL)
  243. {
  244.     fPalette = new CPalette;
  245.     if (fPalette->NumberOfColors() == 256)
  246.     {
  247.         fGrid = new CGrid(8, 32,                     // 8 rows 32 columns
  248.                         FW_CPoint(FW_IntToFixed(2), FW_IntToFixed(2)),
  249.                         FW_CPoint(FW_IntToFixed(8), FW_IntToFixed(8)),
  250.                         FW_kFixedPos1);
  251.     }
  252.     else
  253.     {
  254.         fGrid = new CGrid(2, 8,                     // 2 rows 8 columns
  255.                         FW_CPoint(FW_IntToFixed(2), FW_IntToFixed(2)),
  256.                         FW_CPoint(FW_IntToFixed(16), FW_IntToFixed(16)),
  257.                         FW_kFixedPos1);
  258.     }
  259. }
  260.  
  261. //----------------------------------------------------------------------------------------
  262. // CPaletteFrame::~CPaletteFrame
  263. //----------------------------------------------------------------------------------------
  264.  
  265. CPaletteFrame::~CPaletteFrame()
  266. {
  267.     delete fPalette;
  268.     fPalette = NULL;
  269.     
  270.     delete fGrid;
  271.     fGrid = NULL;
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. // CPaletteFrame::Draw
  276. //----------------------------------------------------------------------------------------
  277.  
  278. void CPaletteFrame::Draw(Environment* ev, ODFacet* odFacet, ODShape* invalidShape)
  279. {    
  280.     FW_CViewContext vc(ev, this, odFacet, invalidShape);
  281.     vc.SetMapping(fMapping);
  282.  
  283.     EraseBackground(ev, vc);
  284.     
  285.     // ----- Draw the grid -----
  286.     fGrid->DrawGridBorders(vc);
  287.     
  288.     // ----- Draw each color -----
  289.     FW_CColor color;
  290.     FW_CRect rect;
  291.     FW_CRectShape rectShape(FW_kZeroRect, FW_kFill);
  292.     for (unsigned long index = 0; index < fPalette->NumberOfColors(); index++)
  293.     {
  294.         fGrid->GetCellInterior(index, rect);
  295.         rectShape.SetRectangle(rect);
  296.         fPalette->GetColor(index, &color);
  297.         rectShape.GetInk().SetForeColor(color);
  298.         rectShape.Render(vc);
  299.     }
  300. }
  301.  
  302. //----------------------------------------------------------------------------------------
  303. // CPaletteFrame::FacetAdded
  304. //----------------------------------------------------------------------------------------
  305. //    When ODWindow moves back to ODFrame we will be able to move this code
  306. //    back to the constructor
  307.  
  308. void CPaletteFrame::FacetAdded(Environment* ev, ODFacet* facet, unsigned short facetCount)
  309. {
  310.     // ----- Call inherited first -----
  311.     CFloatingWindowFrame::FacetAdded(ev, facet, facetCount);
  312.     
  313.     // ----- Resize my Window -----
  314.     FW_CRect gridRect;
  315.     fGrid->GetExteriorGridRect(gridRect);
  316.     gridRect.Inset(-FW_IntToFixed(2), -FW_IntToFixed(2));
  317.     FW_CPoint windowSize(gridRect.Width(), gridRect.Height());
  318.     GetWindow(ev)->SetWindowSize(ev, windowSize);
  319. }
  320.  
  321. //----------------------------------------------------------------------------------------
  322. // CPaletteFrame::DoMouseDown
  323. //----------------------------------------------------------------------------------------
  324.  
  325. FW_Handled CPaletteFrame::DoMouseDown(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  326. {
  327.     FW_CPoint where = theMouseEvent.GetLogicalMousePosition(ev, fMapping);
  328.     
  329.     unsigned long colorIndex;    
  330.     if (fGrid->FindCell(where, colorIndex))
  331.     {
  332.         FW_CColor color;
  333.         fPalette->GetColor(colorIndex, &color);
  334.         
  335.         CColorChangedInterest interest(this);
  336.         CColorChangedNotification notification(interest, color, theMouseEvent.IsCopyModifier(ev) ? FW_kFrame : FW_kFill);
  337.         Notify(ev, notification);
  338.     }
  339.  
  340.     return FW_kHandled;
  341. }
  342.  
  343.